/*******************************************************************************
 * Copyright (c) 2014 Francesco Gabbrielli
 * 
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Public License v2.0
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * 
 * Contributors:
 *     Francesco Gabbrielli - initial API and implementation
 ******************************************************************************/
package it.francescogabbrielli.android.solidalapp;

import it.francescogabbrielli.android.solidalapp.SolidalApp.DataKey;
import android.app.Activity;
import android.os.Bundle;

import com.actionbarsherlock.app.SherlockFragment;

/**
 * Base fragment class that will register itself to the application
 * as a {@link StatusListener} and a {@link DataListener}, much like
 * a {@link BaseActivity}
 *
 * @param <I> The interface that the activity containing this fragment must
 * implement and that will be used to "interact" with the activity. 
 */
public abstract class BaseFragment<I> extends SherlockFragment 
										implements StatusListener, DataListener {
	
	/** Interaction interface to the activity */
	protected I interactionListener;
	
	/** App instance */
	protected SolidalApp app;
	
	public BaseFragment() {
		//setRetainInstance?
	}	
	
	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		try {
			@SuppressWarnings("unchecked")
			I listener = (I) activity;
			this.interactionListener = listener;
			app = (SolidalApp) activity.getApplication();
		} catch (ClassCastException e) {
			throw new ClassCastException(activity.toString()
					+ " must implement the correct interface");
		}
	}
		
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
//		Log.i(getClass().getSimpleName(), "on create: "+savedInstanceState);
	}
	
//	@Override
//	public void onActivityCreated(Bundle savedInstanceState) {
//		
//		super.onActivityCreated(savedInstanceState);
//		
//		if(getActivity()==null)
//			throw new IllegalStateException();
//		
//		// refire events on activity created
//		synchronized (this) {
//			activityOk = true;
//			if (firedEvents!=null) {
//				for (DataEvent event : firedEvents)
//					onDataChanged(event);
//				firedEvents = null;
//			}
//		}
//	}
	
	/**
	 * Register this fragment as a {@link StatusListener} and a {@link DataListener}
	 * to the application.
	 */
	@Override
	public void onResume() {
		super.onResume();
		app.addStatusListener(this);
		app.addDataListener(this);
		
		//fire generic events to "wake up" the fragment
		onStatusUpdate(new StatusEvent());
		onDataChanged();
	}
	
	/**
	 * Unregister from the application
	 */
	@Override
	public void onPause() {
		app.removeDataListener(this);
		app.removeStatusListener(this);
		super.onPause();
	}
		
	@Override
	public void onDetach() {
		interactionListener = null;
//		activityOk = false;
		super.onDetach();
	}

	protected void onDataChanged() {
		for(DataKey key : DataKey.values())
			onDataChanged(new DataEvent(this, key));
	}
	
//	@Override
//	public final synchronized void onSafeDataChanged(DataEvent event) {
//		if (activityOk)
//			onDataChanged(event);
//		else
//			refireOnActivityCreated(event);
//	}
//	
//	private void refireOnActivityCreated(DataEvent event) {
//		if (firedEvents==null)
//			firedEvents = new LinkedList<DataEvent>();
//		firedEvents.add(event);
//	}
		
}
